-- GameCenterAgent
-- Created by chengb Nov/25/2015
-- 负责GameCenter相关逻辑

module("GameCenterAgent", package.seeall);

local PURE_FILE = "GameCenterAgent";

-- 内部函数声明
local onGameCenterAuthFail;
local onGameCenterAuthSucc;
local onManualLoginGameCenter;

function init()
    -- 注册GameCenter认证成功的回调
    SdkMgr.registerOCCallback("onGameCenterAuthSucc", onGameCenterAuthSucc);

    -- 注册GameCenter认证失败的回调
    SdkMgr.registerOCCallback("onGameCenterAuthFail", onGameCenterAuthFail); 

    -- 注册手动登录GameCenter的回调
    SdkMgr.registerOCCallback("onManualLoginGameCenter", onManualLoginGameCenter);   
end

-- GameCenter登录认证成功
onGameCenterAuthSucc = function(args)
    trace(PURE_FILE, "onGameCenterAuthSucc: args = " .. tostring(args));

    local account = args;

    local info = {
        ["account"] = account,
        ["extra"] = {
            ["3rdplatform"] = CHANNEL_GAME_CENTER,
        },
    };

    -- 记录第三方账号信息
    set3rdAccountLoginInfo(CHANNEL_GAME_CENTER, info);

    -- 抛出GameCenter账号登录事件
    EventMgr.fire(event.GAME_CENTER_CONNECTED, { ["loginInfo"] = info, });
end

-- GameCenter认证失败
onGameCenterAuthFail = function()
    EventMgr.fire(event.GAME_CENTER_ERROR);
end

-- 提示 GameCenter 需要手动登录
onManualLoginGameCenter = function()
    showHint2(getLocStr("slime_note"), getLocStr("manual_login_game_center"), true);
end

-- 请求登录GameCenter
function loginGameCenter()
    callOCFuncWithClass("GameCenterWrap", "loginGameCenter");
end

----------------------
-- 提交成就进度
-- @param identifier       成就id
-- @param percentComplete  完成百分比
function submitAchievement(identifier, percentComplete)
    callOCFuncWithClass("GameCenterWrap", "submitAchievement", {
        identifier      = identifier,
        percentComplete = percentComplete,
    });
end

-- 显示成就列表
function showAchievements()
    callOCFuncWithClass("GameCenterWrap", "showAchievements");
end

-- 显示排行榜列表
function showLeaderBoards()
    callOCFuncWithClass("GameCenterWrap", "showLeaderboard", {});
end

-- 显示单个排行榜
function showLeaderBoard(identifier)
    callOCFuncWithClass("GameCenterWrap", "showLeaderboard", {
        identifier = identifier,
    });
end

-- 提交排行榜分数
function reportLeaderboard(identifier, score)
    callOCFuncWithClass("GameCenterWrap", "reportLeaderboard", {
        identifier = identifier,
        score      = score,
    });
end



